﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Zj.HttpCodeDemo.App.Tag.Events;
using Zj.HttpCodeDemo.Domain.Events;

namespace Zj.HttpCodeDemo.App.Tag.EventHandlers
{
    public class UserCreatedEventHandler
        : IEventHandler<UserCreatedEvent>
    {
        private readonly IEventStore _store;
        public UserCreatedEventHandler(IEventStore store)
        {
            this._store = store;
        }
        public bool CanHandle(IEvent @event)
        {
            return typeof(IEvent) == typeof(UserCreatedEvent);
        }

        public async Task<bool> HandleAsync(IEvent @event, CancellationToken cancellationToken = default(CancellationToken))
        {
            return  CanHandle(@event)? await HandleAsync((UserCreatedEvent)@event): await Task.FromResult(false);
        }

        public async Task<bool> HandleAsync(UserCreatedEvent @event, CancellationToken cancellationToken = default(CancellationToken))
        {
            await this._store.SaveAsync(@event);
            return true;
        }
    }
}
